package com.bwie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Brand;
import com.bwie.service.BrandService;
import com.bwie.mapper.BrandMapper;
import com.bwie.utils.R;
import com.bwie.vo.BrandVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
* @author 61630
* @description 针对表【tb_brand】的数据库操作Service实现
* @createDate 2024-08-15 16:20:03
*/
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand>
    implements BrandService{
    @Resource
    private BrandMapper brandMapper;
    @Override
    public R brandList(BrandVo brandVo) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        if(brandVo.getName()!=""){
            wrapper.like(Brand::getName,brandVo.getName());
        }
        if(brandVo.getFirstChar()!=""){
            wrapper.eq(Brand::getFirstChar,brandVo.getFirstChar());
        }
        Page<Brand> brandPage = new Page<>(brandVo.getPageNum(), brandVo.getPageSize());
        Page<Brand> pages = brandMapper.selectPage(brandPage, wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",pages.getRecords());
        map.put("total",pages.getTotal());
        map.put("pages",pages.getPages());
        map.put("size",pages.getSize());
        map.put("current",pages.getCurrent());
        return R.success(map);
    }

    @Override
    public R updatesByid(BrandVo brandVo) {
        Brand brand = BeanUtil.toBean(brandVo, Brand.class);
        brandMapper.updateById(brand);
        return R.success();
    }

    @Override
    public R insertBrand(BrandVo brandVo) {
        Brand brand = BeanUtil.toBean(brandVo, Brand.class);
        brandMapper.insert(brand);
        return R.success();
    }

    @Override
    public R batchDel(List<Integer> brandList) {

        brandMapper.deleteBatchIds(brandList);
        return R.success();
    }
}




